package org.linlinjava.litemall.db.util;

import org.linlinjava.litemall.db.domain.LitemallOrder;
import org.linlinjava.litemall.db.domain.LitemallOrderItem;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/*
 * 订单流程：售卖商品 下单成功－》支付订单－》发货－》收货
 *         租赁商品 下单成功－》支付订单－》发货－》收货 -》归还
 * 子订单状态：前端状态：后台管理状态
 * 101 (101)(101) 订单生成，未支付；
 * 102 (601)(601) 下单未支付用户取消；
 * 103 (701)(701) 下单未支付超期系统自动取消
 * 201 (201)(201) 支付完成，商家未发货；
 * 301 (301)(301) 商家发货，用户未确认 商户待收货；
 * 302 (301)(302) 商家发货，用户未确认 待用户自提；
 * 401 (801)(801) 售卖商品 用户确认收货，订单结束；
 * 402 (801)(801) 售卖商品 用户没有确认收货，但是快递反馈已收货后，超过一定时间，系统自动确认收货，订单结束。
 * 406 (801)(801) 售卖商品 用户拒收，订单结束，开始售后。
 * 403 (401)(401) 租赁商品 用户确认收货；
 * 404 (401)(401) 租赁商品 用户没有确认收货，但是快递反馈已收货后，超过一定时间，系统自动确认收货。
 * 501 (501)(501) 租赁商品 申请归还 待商家取货
 * 502 (501)(502) 租赁商品 申请归还 用户自己送回或寄回 待商家收货
 * 503 (501)(503) 租赁商品 待结算
 * 601 (801)(801)交易完成
 *
 *
 * 前端状态
 * 101 待支付
 * 201 待发货 待配货
 * 301 待收货
 * 401 使用中
 * 501 归还中
 * 601 交易取消
 * 701 交易关闭
 * 801 交易完成
 *
 * 管理后台状态
 * 101 待付款
 * 201 待发货
 * 301 商户待收货
 * 302 用待户自提
 * 401 使用中
 * 501 归还，待商家取货
 * 502 归还，待商家收货
 * 503 待结算
 * 601 交易取消
 * 701 交易关闭
 * 801 交易完成
 *
 * 管理系统个状态判断逻辑说明
 * #待配货 agedmall_logistics_package_info ship_status =1 打包完成
 * #待送货 agedmall_logistics_package_info ship_status =2 配货完成
 * #待取货 agedmall_lease_goods_return_record get_goods_style = 0 and receive_time is null
 * #待维护  agedmall_lease_goods_return_record get_goods_style = 0 and receive_time is not null
 * #待结算 agedmall_agent_settle_detail  proxy_status = 0待结算 1 已结算
 *
 * 当101用户未付款时，此时用户可以进行的操作是 取消或者付款
 * 当102 103 取消时，可以删除
 * 当201 提醒发货
 * 当301商家已发货时，此时用户可以有 确认收货
 * 当401用户确认收货以后，此时用户可以进行的操作是退货、删除、去评价或者再次购买
 * 当402系统自动确认收货以后，此时用户可以删除、去评价、或者再次购买
 */
public class OrderUtil {

    public static final Short STATUS_CREATE = 101;
    public static final Short STATUS_CANCEL = 102;
    public static final Short STATUS_AUTO_CANCEL = 103;
    public static final Short STATUS_PAY = 201;
    public static final Short STATUS_REFUND = 202;
    public static final Short STATUS_REFUND_CONFIRM = 203;
    public static final Short STATUS_SHIP = 301;
    public static final Short STATUS_SHIP_SELF = 302;
    public static final Short STATUS_CONFIRM = 401;
    public static final Short STATUS_AUTO_CONFIRM = 402;
    public static final Short STATUS_LEASE_CONFIRM = 403;
    public static final Short STATUS_LEASE_AUTO_CONFIRM = 404;
    public static final Short STATUS_REFUSED = 406;
    public static final Short STATUS_RETURN = 501;
    public static final Short STATUS_RETURN_SELF = 502;
    public static final Short STATUS_RETURN_SETTLE = 503;
    public static final Short STATUS_RETURN_SUCCESS = 601;
    public static final Short STATUS_ORDER_CANCEL = 701;

    public static String orderStatusText(LitemallOrder order) {
        int status = order.getOrderStatus().intValue();

        if (status == 101) {
            return "未付款";
        }

        if (status == 102) {
            return "已取消";
        }

        if (status == 103) {
            return "已取消(系统)";
        }

        if (status == 201) {
            return "已付款";
        }

        if (status == 202) {
            return "订单取消，退款中";
        }

        if (status == 203) {
            return "已退款";
        }

        if (status == 204) {
            return "已超时团购";
        }

        if (status == 301) {
            return "已发货";
        }

        if (status == 401) {
            return "已收货";
        }

        if (status == 402) {
            return "已收货(系统)";
        }

        throw new IllegalStateException("orderStatus不支持");
    }


    public static OrderHandleOption build(LitemallOrder order) {
        int status = order.getOrderStatus().intValue();
        return buildItemByStatus(status);
    }

    public static OrderHandleOption buildItem(LitemallOrderItem orderItem) {
        int status = orderItem.getOrderStatus().intValue();
        return buildItemByStatus(status);
    }

    public static OrderHandleOption buildItemByStatus(Integer status) {
        OrderHandleOption handleOption = new OrderHandleOption();

        if (status == 101) {
            // 如果订单没有被取消，且没有支付，则可支付，可取消
            handleOption.setCancel(true);
            handleOption.setPay(true);
        } else if (status == 102 || status == 103 || status == 401 || status == 402 || status == 601 || status.intValue() == Integer.valueOf(OrderUtil.STATUS_REFUND_CONFIRM).intValue()) {
            // 如果订单已经取消或是已完成，则可删除
            handleOption.setDelete(true);
        } else if (status == 201 ) {
            // 如果订单已支付，未发货，可提醒发货
            handleOption.setRemind(true);
        } else if (status == 301 || status == 302) {
            // 如果订单已经发货，没有收货，则可确认收货操作,
            handleOption.setConfirm(true);
        } else if (status == 403 || status == 404) {
            // 租赁商品使用中，可申请归还
            handleOption.setReback(true);
        }

        return handleOption;
    }

    /**
     * 前端上传状态映射
     * @param showType
     * @return
     */
    public static List<Short> orderStatus(Integer showType) {
        // 全部订单
        if (null == showType) {
            return null;
        }

        List<Short> status = new ArrayList<Short>(2);
        switch (showType) {
            case 101:
                status.add((short) 101);
                break;
            case 201:
                status.add((short) 201);
                break;
            case 301:
                status.add((short) 301);
                status.add((short) 302);
                break;
            case 401:
                status.add((short) 403);
                status.add((short) 404);
                break;
            case 501:
                status.add((short) 501);
                status.add((short) 502);
                status.add((short) 503);
                break;
            case 601:
                status.add((short) 102);
                break;
            case 701:
                status.add((short) 103);
                status.add((short) 102);
                break;
            case 801:
                status.add((short) 401);
                status.add((short) 402);
                status.add((short) 406);
                status.add((short) 601);
                status.add((short) OrderUtil.STATUS_REFUND_CONFIRM);
                break;
            default:

        }

        return status;
    }

    public static Integer showType(Short orderStatus) {
        if (null == orderStatus) {
            return null;
        }
        Integer status = null;
        switch (orderStatus) {
            case 101:
                status = 101;
                break;
            case 102:
                status = 601;
                break;
            case 103:
                status = 701;
                break;
            case 201:
                status = 201;
                break;
            case 301:
            case 302:
                status = 301;
                break;
            case 401:
            case 402:
            case 406:
            case 601:
            case 203:
                status = 801;
                break;
            case 403:
            case 404:
                status = 401;
                break;
            case 501:
            case 502:
            case 503:
                status = 501;
                break;
            default:
        }

        return status;
    }

    /**
     * 根据后台管理系统页面传递的状态码值获取子订单对应的系统状态值
     * @param adminPageStat
     * @return
     */
    public static Integer getChildSysStatByAdminPageStat(Short adminPageStat, boolean isLease) {
        if (null == adminPageStat) {
            return null;
        }
        Integer status = null;
        switch (adminPageStat) {
            case 101:
                status = 101;
                break;
            case 601:
                status = 102;
                break;
            case 701:
                status = 103;
                break;
            case 201:
                status = 201;
                break;
            case 301:
                status = 301;
                break;
            case 302:
                status = 302;
                break;
            case 801:
                if(isLease) {
                    status = 601;
                }else {
                    status = 402;
                }
                break;
            case 401:
                //租赁商品404
                status = 404;
                break;
            case 501:
                status = 501;
                break;
            case 503:
                status = 503;
                break;
            default:
        }

        return status;
    }

    /**
     * 管理系统页面是否取消订单的状态
     * @param adminPageStat
     * @return
     */
    public static boolean adminIsCancelOrder(int adminPageStat) {
        return adminPageStat == 601 || adminPageStat == 701;
    }

    public static List<Short> backendOrderStatus(Integer showType) {
        // 全部订单
        if (null == showType) {
            return null;
        }

        List<Short> status = new ArrayList<Short>(2);
        switch (showType) {
            case 101:
                status.add((short) 101);
                break;
            case 201:
                status.add((short) 201);
                break;
            case 301:
                status.add((short) 301);
            case 302:
                status.add((short) 302);
                break;
            case 401:
                status.add((short) 403);
                status.add((short) 404);
                break;
            case 501:
                status.add((short) 501);
                break;
            case 502:
                status.add((short) 502);
                break;
            case 503:
                status.add((short) 503);
                break;
            case 601:
                status.add((short) 102);
                break;
            case 701:
                status.add((short) 103);
                status.add((short) 102);
                break;
            case 801:
                status.add((short) 401);
                status.add((short) 402);
                status.add((short) 406);
                status.add((short) 601);
                status.add(STATUS_REFUND_CONFIRM);
                break;
            default:

        }

        return status;
    }

    public static Integer backendShowType(Short orderStatus) {
        if (null == orderStatus) {
            return null;
        }
        Integer status = null;
        switch (orderStatus) {
            case 101:
                status = 101;
                break;
            case 102:
                status = 601;
                break;
            case 103:
                status = 701;
                break;
            case 201:
                status = 201;
                break;
            case 301:
                status = 301;
                break;
            case 302:
                status = 302;
                break;
            case 401:
            case 402:
            case 406:
            case 601:
            case 203:
                status = 801;
                break;
            case 403:
            case 404:
                status = 401;
                break;
            case 501:
                status = 501;
                break;
            case 502:
                status = 502;
                break;
            case 503:
                status = 503;
                break;
            default:
        }

        return status;
    }


    public static boolean isCreateStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_CREATE == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean hasPayed(LitemallOrder order) {
        return OrderUtil.STATUS_CREATE != order.getOrderStatus().shortValue()
                && OrderUtil.STATUS_CANCEL != order.getOrderStatus().shortValue()
                && OrderUtil.STATUS_AUTO_CANCEL != order.getOrderStatus().shortValue();
    }

    public static boolean isPayStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_PAY == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isShipStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_SHIP == litemallOrder.getOrderStatus().shortValue()
                || OrderUtil.STATUS_SHIP_SELF == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isConfirmStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_CONFIRM == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isCancelStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_CANCEL == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isAutoCancelStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_AUTO_CANCEL == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isRefundStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_REFUND == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isRefundConfirmStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_REFUND_CONFIRM == litemallOrder.getOrderStatus().shortValue();
    }

    public static boolean isAutoConfirmStatus(LitemallOrder litemallOrder) {
        return OrderUtil.STATUS_AUTO_CONFIRM == litemallOrder.getOrderStatus().shortValue();
    }
}
